package com.black.opencvStudy;

import org.junit.Test;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.util.Map;

/**
 * @description：
 * @author：8568
 */
public class demo5 extends OpencvEV {

    private static Mat redMat = new Mat(new Size(40, 40), CvType.CV_8UC3, new Scalar(0, 0, 125));
    private static Mat greenMat = new Mat(new Size(40, 40), CvType.CV_8UC3, new Scalar(0, 125));
    private static Mat blueMat = new Mat(new Size(40, 40), CvType.CV_8UC3, new Scalar(125));

    /**
     * 图像加法
    */
    @Test
    public void test() {
        Mat result = new Mat();
        // 红绿叠加
        Core.add(redMat, greenMat, result);
        HighGui.namedWindow("show");
        HighGui.imshow("show", result);
        HighGui.waitKey(0);
        // 红绿蓝叠加
        Core.add(result, blueMat, result);
        HighGui.imshow("show", result);
        HighGui.waitKey(0);

        // 像素点再加上130 (最终显示为白色)
        Core.add(result, new Scalar(130, 130, 130), result);
        HighGui.imshow("show", result);
        HighGui.waitKey(0);
    }

    /**
     * 图像融合
    */
    @Test
    public void addWeighted() {
        Mat img = Imgcodecs.imread("src/main/resources/img/img.jpg", Imgcodecs.IMREAD_COLOR);
        Mat red = new Mat(new Size(img.cols(), img.rows()), CvType.CV_8UC3, new Scalar(0, 0, 255));
        Mat result = new Mat();
        Core.addWeighted(
                // 图片 权重
                img, 0.7,
                // 图片 权重
                red, 0.3,
                // 融合结果再加上的值
                50,
                // 显示结果图片
                result);
        HighGui.namedWindow("show");
        HighGui.imshow("show", result);
        HighGui.waitKey(0);
    }

    /**
     * 图片的位运算
     * @TODO 最终显示效果不对、待排查
    */
    @Test
    public void bitWise() {
        HighGui.namedWindow("show", HighGui.WINDOW_AUTOSIZE);
        Mat img = Imgcodecs.imread("src/main/resources/img/img.jpg", Imgcodecs.IMREAD_COLOR);
        Mat logo= Imgcodecs.imread("src/main/resources/img/logo.png", Imgcodecs.IMREAD_COLOR);
        HighGui.imshow("show", img);
        HighGui.waitKey(0);

        int logoRow = logo.rows();
        int logoCol = logo.cols();

        // 创建img上的roi
        Mat submat = img.submat(new Rect(0, 0, logoCol, logoRow));
        //创建logo的掩码、并同时创建其相反掩码
        Mat logoGray = new Mat();
        Imgproc.cvtColor(logo, logoGray, Imgproc.COLOR_BGR2GRAY);
        Mat logoMask = new Mat();
        double ret = Imgproc.threshold(logoGray, logoMask, 10, 255, Imgproc.THRESH_BINARY);
        Mat mask_inv = new Mat();
        Core.bitwise_not(logoMask, mask_inv);

        // 展示not运算结果
        HighGui.imshow("show", mask_inv);
        HighGui.waitKey(0);

        // 将roi中的logo的logo的区域涂黑
        Mat img_bg = new Mat();
        Core.bitwise_and(submat, submat, img_bg, mask_inv);
        HighGui.imshow("show", img_bg);
        HighGui.waitKey(0);

        // 仅从logo图像中提取有效的logo区域
        Mat logo_fg = new Mat();
        Core.bitwise_and(logo, logo, logo_fg, mask_inv);
        HighGui.imshow("show", logo_fg);
        HighGui.waitKey(0);

        // 将logo放入图片中、
        Mat result = new Mat();
        Core.add(img_bg, logo_fg, result);
        result.copyTo(submat);
        HighGui.imshow("show", img);
        HighGui.waitKey(0);
    }
}
